from google.appengine.api import memcache
import logging

from django import http

from seblog.web.render import render_to_response

logging.getLogger().setLevel(logging.DEBUG)

def get_memcache(cache_key):
    cache_value = memcache.get(cache_key)
    if cache_value is not None:
        return cache_value
    else:
        cache_value = 'test_tag_a'
    if not memcache.add("test_tag", cache_value, 10):
        logging.error("Memcache test_tag set failed.")
    return cache_value

def del_memcache(cache_key):
    memcache.delete(cache_key)
    return True

def get_all_memcache():
    pass
#    print dir(memcache)

def index(request):
    get_all_memcache()
    params = {}
    return render_to_response('demoapp/memcache/index.html', params)

def new(request):
    try:
        key = request.POST.get('key', None)
        value = request.POST.get('value', None)
        print key,value
        print dir(memcache)
        cache_value = memcache.get(key)
        
        if cache_value:
            result = memcache.set(key,value)
        else:
            result = memcache.add(key,value)
        print result
        print cache_value
        return http.HttpResponse('yes')
    except Exception,e:
        print str(e)
        return http.HttpResponse(str(e))
                                 
